///---------------------------------------------------------------------------------------------------------------------------------------------------------///

*REPLICATION CODE FOR ALL TABLES, Desai and Olofgard, "Can the Poor Organize?"

*NOTE: Full replication requires the following user-written Stata commands:  kappa.do; brl.do; cgmwildboot.do; cgmreg.do; psmatch2.do; ebalance.do; rbounds.do

///---------------------------------------------------------------------------------------------------------------------------------------------------------///


*IMPORTANT:  SET PATH BELOW (replace <ETC> with appropriate path)

* For Mac
cd "/Users/<ETC>/"

*For Windows
cd "C:\Users\<ETC>\"


*PART I:  SURVEY ANALYSIS

set more off

*Generate variables and strata

do "Survey Variables.do"
do "Strata.do"


*Table 1:  Balance

foreach i of varlist RoadsQual ElecQual EducQual HealthQual WaterQual PipedWater FarmWater {
	diff `i', treated(SewaV) period(Aft)
	}


*Table A2(B):  Balance
	
foreach i of varlist Age HHead ST SC Literate Married Children Landowner Kutcha {
	ttest `i', by (Treat_before)
	}
 

*Table 2:  Public Goods Quality, Conditional Effects

local depvars "RoadsQual ElecQual EducQual HealthQual WaterQual PipedWater FarmWater"
foreach var of local depvars { 
	xi: regress `var' SewaV Aft SewaVXAft Age HHead ST SC Literate Married Children Landowner Kutcha i.strata, cluster(Vill)
	eststo `var'
}
esttab using  table2.csv, b(3) se(3)  abs order(SewaV Aft SewaVXAft Age HHead ST SC Literate Married Children Landowner Kutcha) stats(r2 N, labels("R-squared" "N")) star(* 0.10 ** 0.05 *** 0.01) label compress  nogaps 
 
 
*Table 4:  Interactions with Officials

local depvars "know_roads contact_roads sarpanch_roads know_elec contact_elec sarpanch_elec know_educ contact_educ sarpanch_educ know_health contact_health sarpanch_health know_water contact_water sarpanch_water"
foreach var of local depvars { 
	xi: regress `var' SewaV Aft SewaVXAft Age HHead ST SC Literate Married Children Landowner Kutcha i.strata, cluster(Vill)
	eststo `var'
}
esttab using  table4.csv,  b(3) se(3)  abs keep(SewaV Aft SewaVXAft) order(SewaV Aft SewaVXAft) stats(r2 N, labels("R-squared" "N")) star(* 0.10 ** 0.05 *** 0.01) label compress  nogaps 


*Table 5:  Intermediate Effects


foreach i of varlist FinalSay_Sch FinalSay_Med FinalSay_FP EngageGSGP VoteLocal VoteState VoteNational SHG BankLoan SavingHabit {
	diff `i', treated(SewaV) period(Aft)
}

local depvars "FinalSay_Sch FinalSay_Med FinalSay_FP EngageGSGP VoteLocal VoteState VoteNational SHG BankLoan SavingHabit"
foreach var of local depvars { 
	xi: regress `var' SewaV Aft SewaVXAft Age HHead ST SC Literate Married Children Landowner Kutcha i.strata, cluster(Vill)
	eststo `var'
}
esttab using  table5.csv,  b(3) se(3)  abs keep(SewaVXAft) stats(r2 N, labels("R-squared" "N")) star(* 0.10 ** 0.05 *** 0.01) label compress  nogaps 


*Table 8: Concordances for rankings and ratings

qui: do "Concordance Scores_Rankings.do"
keep if control0==1
drop id Village control* treatment*
xpose, clear varname
*Control villages, pre-treatment
kap v*

qui: do "Concordance Scores_Rankings.do"
keep if control1==1
drop id Village control* treatment*
xpose, clear varname
*Control villages, post-treatment
kap v*

qui: do "Concordance Scores_Rankings.do"
keep if treatment0==1
drop id Village control* treatment*
xpose, clear varname
*Treatment villages, pre-treatment
kap v*

qui: do "Concordance Scores_Rankings.do"
keep if treatment1==1
drop id Village control* treatment*
xpose, clear varname
*Treatment villages, post-treatment
kap v*

qui: do "Concordance Scores_Rankings.do"
keep if treatmentA1==1
drop id Village control* treatment*
xpose, clear varname
*SHG Members in treatment villages post-treatment
kap v*

qui: do "Concordance Scores_Rankings.do"
keep if treatmentB1==1
drop id Village control* treatment*
xpose, clear varname
*Non-members in treatment villages post-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if control0==1
drop id Village control* treatment*
xpose, clear varname
*Control villages, pre-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if control1==1
drop id Village control* treatment*
xpose, clear varname
*Control villages, post-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if treatment0==1
drop id Village control* treatment*
xpose, clear varname
*Treatment villages, pre-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if treatment1==1
drop id Village control* treatment*
xpose, clear varname
*Treatment villages, post-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if treatmentA1==1
drop id Village control* treatment*
xpose, clear varname
*SHG Members in treatment villages post-treatment
kap v*

qui: do "Concordance Scores_Ratings.do"
keep if treatmentB1==1
drop id Village control* treatment*
xpose, clear varname
*Non-members in treatment villages post-treatment
kap v*


///----------------------------------------------------------------------------///

*PART II:  VILLAGE PANEL ANALYSIS


*Generate variables

do "Village Variables.do"


*Table A2(A):  Tests of Means

local depvars "literate married kutcha st sc"
foreach var of local depvars {
	ttest `var' if after==0, by(sewa)
}


*Table 3: panel-correct standard errors + cross-section

xtpcse pipedwater sewa after sewafter literate married kutcha st sc, pa nmk
outreg using table3, se nocons sdec(3) starlevels(10 5 1) starloc(1) summdec(0 0 3 0 3) summstat(N\N_g\r2\chi2\p) nodisplay replace

xtpcse badaccess sewa after sewafter literate married kutcha st sc, pa nmk
outreg using table3, se nocons sdec(3) starlevels(10 5 1) starloc(1) summdec(0 0 3 0 3) summstat(N\N_g\r2\chi2\p) nodisplay merge

reg dw_tfts sewa literate married kutcha st sc, vce(robust)
outreg using table3, se nocons sdec(3) starlevels(10 5 1) starloc(1) summdec(0 3 3) summstat(N\r2\p) nodisplay merge



///----------------------------------------------------------------------------///

*PART III:  BEHAVIORAL ANALYSIS


do "Games Variables.do"


set more off
set seed 12345


*Table 6:  First round

* Bias-reduced linearization
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: brl `var' SewaVill thresh July2011 age literate if round==1, cluster(villagecode)
	eststo `var'
}
esttab using  table6.csv, b(3) se(3) abs order(SewaVill thresh July2011 age literate) star(* 0.10 ** 0.05 *** 0.01) label compress nogaps 

*Cluster-robust SE
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: qui: regress `var' SewaVill thresh July2011 age literate if round==1, cluster(villagecode)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
}
*Wild-t bootstrap
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: qui: cgmwildboot `var' SewaVill thresh July2011 age literate if round==1, cluster(villagecode) bootcluster(villagecode) null(0 . . . .) reps(5000)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
}

*Table 7:  Full rounds

* Bias-reduced linearization
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: brl `var' SewaVill July2011 thresh age literate r_2-r_9 d_2-d_6, cluster(villagecode)
	eststo `var'_1
	xi: brl `var' SewaVill SewaVXLNet July2011 thresh age literate LNet  r_2-r_8 d_2-d_6, cluster(villagecode)
	eststo `var'_2
}
esttab using  table7.csv, b(3) se(3)  abs order(SewaVill SewaVXLNet thresh July2011 age literate LNet) star(* 0.10 ** 0.05 *** 0.01) label compress nogaps 

*Cluster-robust SE
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: qui: regress `var' SewaVill July2011 thresh age literate r_* d_*, cluster(villagecode)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
	xi: qui: regress `var' SewaVill SewaVXLNet July2011 thresh age literate LNet  r_* d_*, cluster(villagecode)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
}

*Wild-t bootstrap
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: qui: cgmwildboot `var' SewaVill July2011 thresh age literate r_* d_*, null(0 . . . . . . . . . . . . . . . . . . . .) cluster(villagecode) bootcluster(villagecode)  reps(5000)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
	xi: qui: cgmwildboot `var' SewaVill SewaVXLNet July2011 age literate thresh LNet r_* d_*, null(0 0 . . . . . . . . . . . . . . . . . . . . . ) cluster(villagecode) bootcluster(villagecode) reps(5000)
	display %9.4f 2*ttail(e(df_r), abs(_b[SewaVill]/_se[SewaVill]))
}

*Table A4(A & B)

foreach i of varlist age education noeduc literate mar_1 mar_2 {
	ttest `i' if round==1, by (SewaVill)
}

foreach i of varlist age education noeduc literate mar_1 mar_2 {
	ttest `i' if round==1, by (SewaMem)
}

*Table A5(A & B)

set seed 12345
gen u = uniform()
sort u

qui: psmatch2 SewaMem age age2 education literate mar_1 mar_2 if round==1, logit out(contribution) noreplacement

*predict_sewa = predicted members based on age, years of educ, literacy, and marital status 
gen predict_sewa = 0
centile _pscore, centile(50)
replace predict_sewa = 1 if _pscore > r(c_1) & round==1
sort playercode round
replace predict_sewa = 1 if L.predict_sewa==1

*predmember = predicted members in treatment and control villages
gen predmember = 0 if predict_sewa==1 & SewaVill==0
replace predmember = 1 if predict_sewa==1 & SewaVill==1

*prednonmember = predicted non-members in treatment and control villages
gen prednonmember = 0 if predict_sewa==0 & SewaVill==0
replace prednonmember = 1 if predict_sewa==0 & SewaVill==1

*Comparison of predicted members with predicted non-members in C villages
ttest contribution if round==1 & SewaVill==0, by(predict_sewa)
ttest contribution if SewaVill==0, by(predict_sewa)

*Comparison of predicted members with predicted non-members in T villages
ttest contribution if round==1 & SewaVill==1, by(predict_sewa)
ttest contribution if SewaVill==1, by(predict_sewa)

*Comparison of predicted members in T villages with predicted members in C villages
ttest contribution if round==1, by(predmember)
ttest contribution, by(predmember)

*Comparison of predicted non-members in T villages with predicted non-members in C villages
ttest contribution if round==1, by(prednonmember)
ttest contribution, by(prednonmember)

drop _weight


*Table A6:  Entropy rebalancing

set seed 12345

sort playercode round

qui: ebalance SewaMem age age2 education literate mar_1 mar_2, targets(1)

gen weight = _webal if round==1
replace weight = L._webal if round>1

eststo clear
local depvars "contribution payout net"
foreach var of local depvars { 
	xi: brl `var' SewaMem SewaVNonMem thresh July2011 age literate if round==1 [pw=weight], cluster(villagecode)
	eststo `var'
}
esttab using  tableA6.csv, b(3) se(3) keep(SewaMem SewaVNonMem thresh age literate) abs order(SewaMem SewaVNonMem thresh age literate) stats(r2_a N, labels("R-squared" "N")) star(* 0.10 ** 0.05 *** 0.01) label compress nogaps 


local depvars "contribution payout net"
foreach var of local depvars { 
	xi: brl `var' SewaMem SewaVNonMem July2011 thresh age literate r_1-r_8 d_1 d_3-d_4 d_6-d_7 [pw=weight], cluster(villagecode)
	eststo `var'_1
	xi: brl `var' SewaMem SewaVNonMem SewaMemXLNet SewaVNonMemXLNet July2011 thresh age literate LNet r_2-r_7 r_9 d_1-d_3 d_6-d_7 [pw=weight], cluster(villagecode)
	eststo `var'_2
}
esttab using  tableA6.csv, append  b(3) se(3) keep(SewaMem SewaVNonMem SewaMemXLNet SewaVNonMemXLNet thresh age literate LNet) abs order(SewaMem SewaVNonMem SewaMemXLNet SewaVNonMemXLNet thresh age literate LNet) stats(r2_a N, labels("R-squared" "N")) star(* 0.10 ** 0.05 *** 0.01) label compress nogaps 

drop weight


*Table A7:  Rosenbaum bounds

rbounds contribution if round==1, gamma(1 (.25) 2)
rbounds contribution if round==1, gamma(3 (1) 5)
rbounds contribution if round==1, gamma(10 (5) 20)
rbounds contribution if round==1, gamma(30 (10) 80)
